home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / pdftops / goo / c++ / GString < prev   
Text File  |  1996-06-08  |  3KB  |  157 lines

  1. //========================================================================
  2. //
  3. // GString.cc
  4. //
  5. // Simple variable-length string type.
  6. //
  7. // Copyright 1996 Derek B. Noonburg
  8. //
  9. //========================================================================
  10.  
  11. #ifdef __GNUC__
  12. //#pragma implementation
  13. #endif
  14.  
  15. #include <stdlib.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include "GString.h"
  19.  
  20. static inline int size(int len) {
  21.   int delta;
  22.  
  23.   delta = len < 256 ? 7 : 255;
  24.   return ((len + 1) + delta) & ~delta;
  25. }
  26.  
  27. inline void GString::resize(int length1) {
  28.   char *s1;
  29.  
  30.   if (!s) {
  31.     s = new char[size(length1)];
  32.   } else if (size(length1) != size(length)) {
  33.     s1 = new char[size(length1)];
  34.     memcpy(s1, s, length + 1);
  35.     delete[] s;
  36.     s = s1;
  37.   }
  38. }
  39.  
  40. GString::GString() {
  41.   s = NULL;
  42.   resize(length = 0);
  43.   s[0] = '\0';
  44. }
  45.  
  46. GString::GString(char *s1) {
  47.   int n = strlen(s1);
  48.  
  49.   s = NULL;
  50.   resize(length = n);
  51.   memcpy(s, s1, n + 1);
  52. }
  53.  
  54. GString::GString(char *s1, int length1) {
  55.   s = NULL;
  56.   resize(length = length1);
  57.   memcpy(s, s1, length * sizeof(char));
  58.   s[length] = '\0';
  59. }
  60.  
  61. GString::GString(GString *str) {
  62.   s = NULL;
  63.   resize(length = str->getLength());
  64.   memcpy(s, str->getCString(), length + 1);
  65. }
  66.  
  67. GString::GString(GString *str1, GString *str2) {
  68.   int n1 = str1->getLength();
  69.   int n2 = str2->getLength();
  70.  
  71.   s = NULL;
  72.   resize(length = n1 + n2);
  73.   memcpy(s, str1->getCString(), n1);
  74.   memcpy(s + n1, str2->getCString(), n2 + 1);
  75. }
  76.  
  77. GString::~GString() {
  78.   delete[] s;
  79. }
  80.  
  81. GString *GString::clear() {
  82.   resize(0);
  83.   s[length = 0] = '\0';
  84.   return this;
  85. }
  86.  
  87. GString *GString::append(char c) {
  88.   resize(length + 1);
  89.   s[length++] = c;
  90.   s[length] = '\0';
  91.   return this;
  92. }
  93.  
  94. GString *GString::append(GString *str) {
  95.   int n = str->getLength();
  96.  
  97.   resize(length + n);
  98.   memcpy(s + length, str->getCString(), n + 1);
  99.   length += n;
  100.   return this;
  101. }
  102.  
  103. GString *GString::append(char *str) {
  104.   int n = strlen(str);
  105.  
  106.   resize(length + n);
  107.   memcpy(s + length, str, n + 1);
  108.   length += n;
  109.   return this;
  110. }
  111.  
  112. GString *GString::insert(int i, char c) {
  113.   int j;
  114.  
  115.   resize(length + 1);
  116.   for (j = length + 1; j > i; --j)
  117.     s[j] = s[j-1];
  118.   s[i] = c;
  119.   ++length;
  120.   return this;
  121. }
  122.  
  123. GString *GString::insert(int i, GString *str) {
  124.   int n = str->getLength();
  125.   int j;
  126.  
  127.   resize(length + n);
  128.   for (j = length; j >= i; --j)
  129.     s[j+n] = s[j];
  130.   memcpy(s+i, str->getCString(), n);
  131.   length += n;
  132.   return this;
  133. }
  134.  
  135. GString *GString::insert(int i, char *str) {
  136.   int n = strlen(str);
  137.   int j;
  138.  
  139.   resize(length + n);
  140.   for (j = length; j >= i; --j)
  141.     s[j+n] = s[j];
  142.   memcpy(s+i, str, n);
  143.   length += n;
  144.   return this;
  145. }
  146.  
  147. GString *GString::del(int i, int n) {
  148.   int j;
  149.  
  150.   if (n > 0) {
  151.     for (j = i; j <= length - n; ++j)
  152.       s[j] = s[j + n];
  153.     resize(length -= n);
  154.   }
  155.   return this;
  156. }
  157.